import { Injectable } from '@angular/core';
import { BaseService } from '@core/routes-services/base.service';
import { CacheService } from '@core/routes-services/cache.service';
import { HttpClient } from '@angular/common/http';

/**
 * otc店铺 服务
 */
@Injectable()
export class OtcShopService extends BaseService {

  constructor(http: HttpClient, cacheService: CacheService) {
    super(http, cacheService);
  }

  base_url: any = 'api/otcShops';


  /**
   * 分页
   *
   * @param user
   * @returns {Promise<any>}
   */
  pagination(page: any, keyword: any, status: any, centerId: any): Promise<any> {
    const url = this.base_url + '/pagination';
    if (keyword) {
      page['_filter_like_keyword'] = keyword;
    }
    if (status) {
      page['_filter_eq_status'] = status;
    }
    if (centerId) {
      page['_filter_eq_centerId'] = centerId;
    }
    return this.post(url, page);
  }

  /**
   * 用户关联店铺分页
   *
   * @param user
   * @returns {Promise<any>}
   */
  cusPagination(page: any, keyword: any, status: any, customerId: any): Promise<any> {
    const url = this.base_url + '/cus/pagination';
    if (keyword) {
      page['_filter_like_title'] = keyword;
    }
    if (status) {
      page['_filter_eq_status'] = status;
    }
    if (customerId) {
      page['_filter_eq_customerId'] = customerId;
    }
    return this.post(url, page);
  }

  /**
   * 修改
   *
   * @param id
   * @param entity
   * @returns {Promise<any>}
   */
  saveOrUpdateEntity(id: any, entity: any): Promise<any> {
    if (id) {
      const url = this.base_url + '/' + id;
      return this.put(url, entity);
    } else {
      const url = this.base_url;
      //后台管理新增的店铺默认审核通过
      entity.auditingStatus = 3;
      return this.post(url, entity);
    }
  }

  /**
   * 获取查看详情
   *
   * @param id
   * @returns {Promise<any>}
   */
  detail(id: any): Promise<any> {
    const url = this.base_url + '/' + id;
    return this.get(url);
  }

  /**
   *获取店铺奖励规则
   * @param shopId
   * @param brandId
   * @returns {Promise<any>}
   */
  getRules(shopId: any, brandId: any): Promise<any> {
    const url = this.base_url + '/actions/getRules/' + shopId + '/' + brandId;
    return this.get(url);
  }

  /**
   * 更新门店规则
   * @param shopId
   * @param brandId
   * @param entity
   * @returns {Promise<any>}
   */
  updateRules(shopId: any, brandId: any, entity: any): Promise<any> {
    const url = this.base_url + '/actions/updateRules/' + shopId + '/' + brandId;
    return this.put(url, entity);
  }

  /**
   * 上下线
   *
   * @param ids
   * @returns {Promise<any>}
   */
  onsaleEntity(id): Promise<any> {
    const url = this.base_url + '/auditing/' + id;
    return this.put(url, null);
  }

  /**
   * 审核通过
   * @param id
   * @returns {Promise<any>}
   */
  confirmAuditing(id: any): Promise<any> {
    const url = this.base_url + '/confirm/auditing/' + id;
    return this.put(url, {});
  }

  /**
   * 审核不通过
   * @param id
   * @returns {Promise<any>}
   */
  auditing(id: any, entity: any): Promise<any> {
    const url = this.base_url + '/reject/auditing/' + id;
    return this.put(url, { auditingReason: entity.remark });
  }


  /**
   * 更新用户在店铺的角色
   * @param relateId
   * @param role
   * @returns {Promise<any>}
   */
  updateCusShopRole(relateId: any, role: any): Promise<any> {
    const url = this.base_url + '/actions/update/' + relateId + '/role/' + role;
    return this.put(url, {});
  }
}
